package cate.game.role.bag.num;

import cate.common.table.d.GDBag;
import cate.common.table.d.GDObj;
import cate.common.table.item.num.NumResRow;
import cate.game.client.msg.MergeMsgSender;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.bag.BagContainer;
import cate.game.role.bag.item.msg.ItemAddResp;
import cate.game.role.bag.num.msg.NumResAddResp;

/**
 *  数值资源背包
 */
public class NumResBag extends BagContainer<NumRes> {
    public NumResBag() {}

    public NumResBag(Role role) {
        super(role);
    }

    @Override
    protected byte getItemType() {
        return GDObj.Type.NUM;
    }

    @Override
    public int calcCapacity() {
        return GDBag.Capcity.NUM_RES;
    }

    @Override
    protected ItemAddResp createItemAddMsg() {
        return new NumResAddResp();
    }

    @Override
    protected NumRes createItem(int tid, long num) {
        return new NumRes(role, tid, num);
    }

    @Override
    public NumResRow getItemTpl(int templateId) {
        return role.getGame().table.item.numRes.get(templateId);
    }

    /** 统计m0 */
    public long countM0() {
        return this.count(GDObj.Num.M0);
    }

    /** 增加m0 */
    public void addM0(long addValue) {
        addM0(addValue, null);
    }

    /** 增加m0 */
    public void addM0(long addValue, MergeMsgSender sender) {
        addValue(GDObj.Num.M0, addValue, sender);
    }

    private void addValue(int id, long addValue){
        addValue(id, addValue, null);
    }

    private void addValue(int id, long addValue, MergeMsgSender sender){
        if(addValue < 0){
            new MixRes(GDObj.getMixResStr(GDObj.Type.NUM, id, Math.abs(addValue))).consume(this.getRole(), null, sender);
        }else{
            new MixRes(GDObj.getMixResStr(GDObj.Type.NUM, id, addValue)).add(this.getRole(), sender);
        }
    }

    /** 统计m1 */
    public long countM1() {
        return this.count(GDObj.Num.M1);
    }

    /** 增加m1 */
    public void addM1(long addValue) {
        addValue(GDObj.Num.M1, addValue);
    }

    /** 统计英雄经验 */
    public long countHeroExp() {
        return this.count(GDObj.Num.HERO_EXP);
    }

    /** 增加英雄经验 */
    public void addHeroExp(long addValue) {
        addHeroExp(addValue, null);
    }

    /** 增加英雄经验 */
    public void addHeroExp(long addValue, MergeMsgSender sender) {
        addValue(GDObj.Num.HERO_EXP, addValue, sender);
    }

    /** 统计代币 */
    public long countToken() {
        return this.count(GDObj.Num.DIAMAND);
    }

    /** 增加代币 */
    public void addToken(long addValue) {
        addValue(GDObj.Num.DIAMAND, addValue);
    }

    @Override
    public void onItemDecreased(int tid, long num){
        super.onItemDecreased(tid, num);
        role.getHistory().action.costResValue(num, tid);
    }
}